#include "../include/net/Network.h"
#include "../include/util/Util.h"

#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <cstdlib>
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;

//测试Network::socket()方法
void testSocket()
{

	int fdTCP, fdUDP;

	fdTCP = Network::socket();
	if (Network::close(fdTCP))
	{
		cout << "关闭TCP的fd" << fdTCP << "成功" << endl;
	}

	fdUDP = Network::socket(Network::UDP);
	if (Network::close(fdUDP))
	{
		cout << "关闭UDP的fd" << fdUDP << "成功" << endl;
	}
}

//测试设置fd为非阻塞模式
void testSetNonBlocking()
{
	int fd = Network::socket();
	cout << Network::setNonBlocking(fd) << endl;
}

/*//测试非阻塞读数据
void testRecvBytes()
{
	//写一个简单的server来测试

}*/

//测试绑定端口
void testBind()
{
	int fd = Network::socket();
	if (Network::bind(fd, 16300))
	{
		cout << "绑定成功！" << endl;
	}
	else
	{
		cout << "绑定失败：" << Util::getInstance()->getErrMsg() << endl;
	}

	Network::close(fd);
}

void testAccept()
{
	int serverFd = Network::socket();
	/*

	 if (!Network::setNonBlocking(serverFd)) {
	 cout << "设置非阻塞失败" << endl;
	 exit(-1);
	 }

	 */
	if (!Network::bind(serverFd, 16300))
	{
		cout << "绑定失败：" << Util::getInstance()->getErrMsg() << endl;
		exit(-1);
	}
	if (!Network::listen(serverFd))
	{
		cout << "监听失败：" << Util::getInstance()->getErrMsg() << endl;
		exit(-1);
	}

	while (true)
	{
		int fd;
		EasyAddr addr;
		if ((fd = Network::accept(serverFd, addr)) != -1)
		{
			cout << addr.ip << ":" << addr.port << "连接." << endl;

			Network::setNonBlocking(fd);

			string buf;
			long last = 0;

			bool eof = false;
			while (!eof)
			{
				if (!Network::recvBytes(fd, buf, eof))
				{
					cout << "读取出错" << endl;
					exit(-1);
				}
				else
				{
					cout << "读取了" << buf.size() - last << "字节" << endl;
					last = buf.size();
				}
			}
			cout << "总共读取了" << buf.size() << "字节" << endl;
			cout << buf << endl << endl;
		}
		else
		{
			cout << "连接失败" << Util::getInstance()->getErrMsg() << endl;
		}
	}

	Network::close(serverFd);

}

//用Echo测试recv和send
void testSend()
{
	int serverFd = Network::socket();
	/*

	 if (!Network::setNonBlocking(serverFd)) {
	 cout << "设置非阻塞失败" << endl;
	 exit(-1);
	 }

	 */
	if (!Network::bind(serverFd, 16300))
	{
		cout << "绑定失败：" << Util::getInstance()->getErrMsg() << endl;
		exit(-1);
	}
	if (!Network::listen(serverFd))
	{
		cout << "监听失败：" << Util::getInstance()->getErrMsg() << endl;
		exit(-1);
	}

	while (true)
	{
		int fd;
		EasyAddr addr;
		if ((fd = Network::accept(serverFd, addr)) != -1)
		{
			cout << addr.ip << ":" << addr.port << "连接." << endl;

			Network::setNonBlocking(fd);

			string buf;

			bool eof = false;
			while (!eof)
			{
				if (!Network::recvBytes(fd, buf, eof))
				{
					cout << "读取出错" << endl;
					exit(-1);
				}
				else
				{
					//把读取到的回发
					unsigned long left = buf.size();
					while (true)
					{
						if (!Network::sendBytes(fd, buf, left))
						{
							//出错
							cout << "send出错！" << endl;
						}
						else
						{
							//没错，检查是否发送完了
							if (left == 0)
							{
								//发送完毕
								break;
							}
							else
							{
								cout << "还剩下" << left << "字节" << endl;
							}
						}
					}
					//清空buf
					buf.clear();
				}
				sleep(1);
			}
			cout << "断开连接" << endl;
		}
		else
		{
			cout << "连接失败" << Util::getInstance()->getErrMsg() << endl;
		}
	}

	Network::close(serverFd);

}

//测试地址转换函数
void testAddr()
{
	EasyAddr easy;
	easy.ip = "194.33.67.3";
	easy.port = 33052;

	sockaddr_in addr;
	if (!Network::sockAddr(easy, &addr))
	{
		cout << "AddrEasy to AddrNet fail." << endl;
	}
	EasyAddr e2 = Network::easyAddr(addr);
	cout << e2.ip << ":" << e2.port << endl;
}
/*

 int main() {

 //testSocket();
 //testSetNonBlocking();
 //testBind();
 //testAccept();
 //testAddr();
 testSend();
 return 0;
 }
 */
